//package com.kehutong.house.app;
//
//import java.util.List;
//
//import org.coraframework.inject.Inject;
//import org.coraframework.json.JSONObject;
//import org.coraframework.mvc.MvcAction;
//import org.coraframework.mvc.http.ReqMapping;
//import org.coraframework.mvc.http.result.Result;
//import org.coraframework.orm.Pool;
//import org.coraframework.orm.jdbc.SQLSession;
//import org.coraframework.util.EnumUtil;
//import org.coraframework.util.Objects;
//import org.coraframework.util.time.TimeUtil;
//
//import com.kehutong.house.entity.Community;
//import com.kehutong.house.entity.Resident;
//import com.kehutong.house.entity.House;
//import com.kehutong.house.enums.CustomerType;
//import com.kehutong.message.entity.SmsCode;
//import com.kehutong.message.service.IspService;
//import com.kehutong.session.entity.Session;
//import com.kehutong.webchat.service.WebChatService;
//
//
//@MvcAction
//@ReqMapping("/app/house")
//public class HouseAction {
//
//    @Inject
//    private SQLSession sqlSession;
//    @Inject
//    private IspService ispService;
//    @Inject
//    private WebChatService webChatService;
//
//    @ReqMapping("/list")
//    public Object list(JSONObject jsonObject) {
//        List<House> houses = sqlSession.findArray(House.class)
//                .eq(jsonObject, "companyNo", "communityNo", "buildingNo")
//                .eq("deleted", false)
//                .order("buildingName","ASC")
//                .order("unitName","ASC")
//                .order("floorName","ASC")
//                .order("name","ASC")
//                .exe();
//
//        houses.forEach(item->{
//            String name = item.getOwnerName();
//            if (name.length() > 2) {
//                item.setOwnerName(name.substring(0, 1) + "*" + name.substring(2));
//            }else {
//                item.setOwnerName(name.substring(0, 1) + "*");
//            }
//        });
//
//        return Result.success(houses);
//    }
//
//    @ReqMapping("/get")
//    public Object get(JSONObject jsonObject) {
//        House house = Pool.get(House.class,  jsonObject.getString("id"));
//
//        Community community = Pool.get(Community.class, house.getCommunityNo());
//        house.setCommunityName(community.getName());
//
//        return Result.success(house);
//    }
//
//
//    @ReqMapping("/bind")
//    public Object bind(Session session, JSONObject jsonObject) {
//        Object result = checkSmsCode(session.getUserNo(), jsonObject.getString("code"));
//        if (Objects.nonNull(result)) {
//            return result;
//        }
//
//        House house = Pool.get(House.class, jsonObject.getString("id"));
//        if (Objects.isEmpty(house.getOwnerMobile())) {
//            return Result.error(0, "当前房屋物业方暂未配置业主手机号");
//        }
//        
//        CustomerType userType = EnumUtil.getEnum(CustomerType.class, jsonObject.getString("userType"));
//        if (Objects.isNull(userType)) {
//        	return Result.error(0, "您录入的业主信息不正确");
//        }
//
//        Object rs = webChatService.bindWebChat(session.getUserNo(), jsonObject);
//        if (Objects.nonNull(rs)) {
//        	return rs;
//        }
//        		
//        String userMobile = jsonObject.getString("userMobile");
//        // 业主
//        if (userType == CustomerType.OWNER) {
//        	if (!house.getOwnerMobile().equals(userMobile)) {
//        		return Result.error(0, "您录入的业主信息不正确.");
//        	}
//            jsonObject.put("userName", house.getOwnerName());
//        } else {
//            if (!house.getOwnerMobile().equals(jsonObject.getString("ownerMobile"))) {
//                return Result.error(0, "您录入的业主信息不正确..");
//            }
//        }
//
//        Resident webchatHouse = sqlSession.findOne(Resident.class)
//                .eq("contactId", session.getUserNo())
//                .eq("houseId", house.getId())
//                .eq("deleted",false)
//                .exe();
//        if (Objects.nonNull(webchatHouse)) {
//            return Result.error(0, "您已经绑定过该房间信息");
//        }
//
//        webchatHouse = Pool.newInstance(Resident.class);
//        webchatHouse.setUserName(jsonObject.getString("userName"));
//        webchatHouse.setUserMobile(jsonObject.getString("userMobile"));
//        webchatHouse.setUserType(userType);
//        webchatHouse.setContactId(session.getUserNo());
//        webchatHouse.setHouseId(house.getId());
//        List<Resident> houseList = sqlSession.findArray(Resident.class)
//        		.eq("contactId", session.getUserNo())
//        		.eq("def", true)
//        		.eq("deleted", false)
//        		.exe();
//        
//        if (Objects.isEmpty(houseList)) {
//        	webchatHouse.setDef(true);
//        } else {
//        	webchatHouse.setDef(false);
//        }
//        
//        sqlSession.insert(webchatHouse);
//        return Result.success();
//    }
//
//    private Object checkSmsCode(String userNo, String code){
//        SmsCode smsCode = ispService.find(userNo, code);
//        if (Objects.isNull(smsCode)) {
//            return Result.error(0, "验证码错误");
//        }
//
//        long s = (TimeUtil.timeMillis() - smsCode.getCreateTime().getTime()) / (1000 * 60);
//        if(s > 3){
//            return Result.error(0, "验证码已超时");
//        }
//
//        return null;
//    }
//}
